package com.nvt.core.dao.de;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oracle.jdbc.OracleTypes;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;

public class GenericResultSetExtractor implements
		ResultSetExtractor<List<Map<String, Object>>> {

	@Override
	public List<Map<String, Object>> extractData(ResultSet rs)
			throws SQLException, DataAccessException {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Object object = null;
		int nuColumns = rs.getMetaData().getColumnCount();

		while (rs.next()) {
			Map<String, Object> map = new HashMap<String, Object>();
			for (int i = 1; i <= nuColumns; i++) {
				int type = rs.getMetaData().getColumnType(i);
				switch (type) {
				case OracleTypes.NUMBER:
					object = rs.getInt(i);
					break;
				case OracleTypes.VARCHAR:
					object = rs.getString(i);
					break;
				default:
					object = rs.getObject(i);
					break;
				}
				map.put(rs.getMetaData().getColumnName(i), object);
			}
			list.add(map);
		}

		return list;
	}

}
